/*
 * MainGUI.java
 *
 * Created on 26 de Setembro de 2008, 09:41
 */
package gui;

import ctr.Control;
import ctr.Setting;
import java.util.ResourceBundle;
import javax.swing.JFileChooser;
import javax.swing.UIManager;
/**
 *
 * @author  administrador
 */
public class MainGUI extends javax.swing.JFrame {

    private static final ResourceBundle language = java.util.ResourceBundle.getBundle("lang/language");
    private static MainGUI instance = null;
    private Setting settings;
    private String pathToConvert;
    /** Creates new form MainGUI */
    public MainGUI() {
        setLookAndFeel();
        setLocation(300, 300);
        initComponents();
    }

    public static MainGUI getInstance() {
        if ( instance == null ) {
            instance = new MainGUI();
        }
        return instance;
    }
    private void setLookAndFeel() {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception e) {
            System.err.print(java.util.ResourceBundle.getBundle("lang/language").getString("error_init"));
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        panelFile = new javax.swing.JPanel();
        openFileButton = new javax.swing.JButton();
        openDirButton = new javax.swing.JButton();
        convertButton = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        panelControl = new javax.swing.JPanel();
        controlButtonPanel = new javax.swing.JPanel();
        helpButton = new javax.swing.JButton();
        closeButton = new javax.swing.JButton();
        controlStatusPanel = new javax.swing.JPanel();
        statusLabel = new javax.swing.JLabel();
        principalPanel = new javax.swing.JPanel();
        principalIn = new javax.swing.JPanel();
        principalPanelLeft = new javax.swing.JPanel();
        fpLabel1 = new javax.swing.JLabel();
        csvSeparatorField = new javax.swing.JTextField();
        fpLabel2 = new javax.swing.JLabel();
        encodeFileSelect = new javax.swing.JComboBox();
        principalPanelCenter = new javax.swing.JPanel();
        ppcLabel1 = new javax.swing.JLabel();
        exportTypeSqlField = new javax.swing.JComboBox();
        createTableCheck = new javax.swing.JCheckBox();
        ppcLabel2 = new javax.swing.JLabel();
        strDelimiterSelect = new javax.swing.JComboBox();
        princialOut = new javax.swing.JPanel();
        logScrollPane = new javax.swing.JScrollPane();
        logTextPane = new javax.swing.JTextPane();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        panelFile.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        panelFile.setLayout(new java.awt.GridLayout(8, 1, 2, 2));

        java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("lang/language"); // NOI18N
        openFileButton.setText(bundle.getString("OpenFile")); // NOI18N
        panelFile.add(openFileButton);

        openDirButton.setText(bundle.getString("OpenDir")); // NOI18N
        openDirButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                openDirButtonActionPerformed(evt);
            }
        });
        panelFile.add(openDirButton);

        convertButton.setText(bundle.getString("Convert")); // NOI18N
        convertButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                convertButtonActionPerformed(evt);
            }
        });
        panelFile.add(convertButton);

        jButton1.setText(bundle.getString("ClearLog")); // NOI18N
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        panelFile.add(jButton1);

        getContentPane().add(panelFile, java.awt.BorderLayout.LINE_END);

        panelControl.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        panelControl.setLayout(new java.awt.BorderLayout());

        helpButton.setText(language.getString("Help")); // NOI18N
        helpButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                helpButtonActionPerformed(evt);
            }
        });
        controlButtonPanel.add(helpButton);

        closeButton.setText(bundle.getString("Close")); // NOI18N
        closeButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                closeButtonActionPerformed(evt);
            }
        });
        controlButtonPanel.add(closeButton);

        panelControl.add(controlButtonPanel, java.awt.BorderLayout.EAST);

        controlStatusPanel.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT, 5, 10));

        statusLabel.setText("<html><b> CSV2SQL Java </b> <i>Version 0.1 - Milestone release</i> joao_neto@ymail.com");
        controlStatusPanel.add(statusLabel);

        panelControl.add(controlStatusPanel, java.awt.BorderLayout.CENTER);

        getContentPane().add(panelControl, java.awt.BorderLayout.SOUTH);

        principalPanel.setLayout(new java.awt.GridLayout(2, 1, 2, 2));

        principalIn.setLayout(new java.awt.GridLayout(1, 3, 1, 0));

        principalPanelLeft.setBorder(javax.swing.BorderFactory.createTitledBorder("File properties"));
        principalPanelLeft.setLayout(new java.awt.GridBagLayout());

        fpLabel1.setText(bundle.getString("l_csvSeparator")); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.fill = java.awt.GridBagConstraints.VERTICAL;
        gridBagConstraints.insets = new java.awt.Insets(3, 3, 3, 3);
        principalPanelLeft.add(fpLabel1, gridBagConstraints);

        csvSeparatorField.setText(",");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.ipadx = 30;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(3, 4, 3, 4);
        principalPanelLeft.add(csvSeparatorField, gridBagConstraints);

        fpLabel2.setText(bundle.getString("l_FilesEncoding:")); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.insets = new java.awt.Insets(3, 3, 3, 3);
        principalPanelLeft.add(fpLabel2, gridBagConstraints);

        encodeFileSelect.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Auto-detect", "UTF-8", "ISO-8859-1" }));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.insets = new java.awt.Insets(3, 3, 3, 3);
        principalPanelLeft.add(encodeFileSelect, gridBagConstraints);

        principalIn.add(principalPanelLeft);

        principalPanelCenter.setBorder(javax.swing.BorderFactory.createTitledBorder("SQL properties"));
        principalPanelCenter.setLayout(new java.awt.GridBagLayout());

        ppcLabel1.setText(bundle.getString("l_ExportTo")); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(3, 8, 3, 8);
        principalPanelCenter.add(ppcLabel1, gridBagConstraints);

        exportTypeSqlField.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "SQL Default", "PostGreSQL 8.3", "MySQL 5.5", "MSSQL", "Oracle" }));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.insets = new java.awt.Insets(3, 0, 3, 3);
        principalPanelCenter.add(exportTypeSqlField, gridBagConstraints);

        createTableCheck.setText("CREATE TABLE");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(3, 3, 3, 3);
        principalPanelCenter.add(createTableCheck, gridBagConstraints);

        ppcLabel2.setText(bundle.getString("l_StringDelimiter")); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.insets = new java.awt.Insets(0, 5, 0, 5);
        principalPanelCenter.add(ppcLabel2, gridBagConstraints);

        strDelimiterSelect.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "\"", "'" }));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(3, 0, 3, 3);
        principalPanelCenter.add(strDelimiterSelect, gridBagConstraints);

        principalIn.add(principalPanelCenter);

        principalPanel.add(principalIn);

        princialOut.setLayout(new java.awt.BorderLayout());

        logScrollPane.setViewportView(logTextPane);

        princialOut.add(logScrollPane, java.awt.BorderLayout.CENTER);

        principalPanel.add(princialOut);

        getContentPane().add(principalPanel, java.awt.BorderLayout.CENTER);

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void helpButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_helpButtonActionPerformed
    final HelpDialog dialog = new HelpDialog(new javax.swing.JFrame(), true);
    dialog.addWindowListener(new java.awt.event.WindowAdapter() {

        public void windowClosing(java.awt.event.WindowEvent e) {
            dialog.dispose();
        }
    });
    dialog.setVisible(true);// TODO add your handling code here:
}//GEN-LAST:event_helpButtonActionPerformed

private void openDirButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_openDirButtonActionPerformed
    //select folder
    JFileChooser fc = new JFileChooser();
    fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
    int returnVal = fc.showOpenDialog(this);
    if (returnVal == JFileChooser.APPROVE_OPTION) {
        pathToConvert = fc.getSelectedFile().getAbsolutePath();
    }
    
}//GEN-LAST:event_openDirButtonActionPerformed

private void convertButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_convertButtonActionPerformed
    Control c = Control.getInstance();
    settings = new Setting();
    settings.setFile_csvSeparator(csvSeparatorField.getText());
    settings.setFile_fileEncoding(encodeFileSelect.getSelectedItem().toString());
    settings.setSql_createSQL(createTableCheck.isSelected());
    settings.setSql_exportTo(exportTypeSqlField.getSelectedItem().toString());
    settings.setSql_strDelimiter(strDelimiterSelect.getSelectedItem().toString());
    c.setPreferences(settings);
    c.convert(pathToConvert);
}//GEN-LAST:event_convertButtonActionPerformed

private void closeButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_closeButtonActionPerformed
    System.out.println ( "Exit command");
    System.exit(0);
}//GEN-LAST:event_closeButtonActionPerformed

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
    logTextPane.setText("");
}//GEN-LAST:event_jButton1ActionPerformed

    public void addTextTextBox(final String text) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                logTextPane.setText(logTextPane.getText() +text+"\n");
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton closeButton;
    private javax.swing.JPanel controlButtonPanel;
    private javax.swing.JPanel controlStatusPanel;
    private javax.swing.JButton convertButton;
    private javax.swing.JCheckBox createTableCheck;
    private javax.swing.JTextField csvSeparatorField;
    private javax.swing.JComboBox encodeFileSelect;
    private javax.swing.JComboBox exportTypeSqlField;
    private javax.swing.JLabel fpLabel1;
    private javax.swing.JLabel fpLabel2;
    private javax.swing.JButton helpButton;
    private javax.swing.JButton jButton1;
    private javax.swing.JScrollPane logScrollPane;
    private javax.swing.JTextPane logTextPane;
    private javax.swing.JButton openDirButton;
    private javax.swing.JButton openFileButton;
    private javax.swing.JPanel panelControl;
    private javax.swing.JPanel panelFile;
    private javax.swing.JLabel ppcLabel1;
    private javax.swing.JLabel ppcLabel2;
    private javax.swing.JPanel princialOut;
    private javax.swing.JPanel principalIn;
    private javax.swing.JPanel principalPanel;
    private javax.swing.JPanel principalPanelCenter;
    private javax.swing.JPanel principalPanelLeft;
    private javax.swing.JLabel statusLabel;
    private javax.swing.JComboBox strDelimiterSelect;
    // End of variables declaration//GEN-END:variables
}
